﻿Imports System.Drawing
Imports System.Windows.Forms.AxHost
Imports DERRA.ElimilateMissionScript
Imports GTA
Imports GTA.Math
Imports LemonUI
Imports LemonUI.Elements
Public MustInherit Class Mission
    Public Shared Sub DisableWantedLevel()
        Game.MaxWantedLevel = 0
        Game.Player.WantedLevel = 0
    End Sub
    Public Shared Sub ResetWantedLevel()
        If Not Game.IsMissionActive Then
            Game.MaxWantedLevel = 5
        End If
    End Sub
    Public Shared Sub DisplayMissionCompleteMessage()
        Dim x As Integer = UI.Screen.MainWindowResolution.Width / 2
        Dim frame As ScaledRectangle = New ScaledRectangle(New PointF(x - 1000, 335), New SizeF(2000, 250))
        frame.Color = Color.FromArgb(140, 10, 10, 10)
        Dim text As ScaledText = New ScaledText(New PointF(x, 350), "任务完成")
        text.Color = Color.DarkGoldenrod
        text.Alignment = UI.Alignment.Center
        text.Font = UI.Font.Pricedown
        text.Scale = 2
        Dim s As Sound = New Sound("REMOTE_PLYR_CASH_COUNTER_COMPLETE", "DLC_HEISTS_GENERAL_FRONTEND_SOUNDS")
        s.PlayFrontend()
        Dim timer As Stopwatch = New Stopwatch()
        timer.Start()
        While True
            If timer.Elapsed.TotalSeconds > 4 Then
                Exit While
            End If
            Script.Wait(1)
            frame.Draw()
            text.Draw()
        End While
    End Sub
    Public MustOverride Sub Start()
End Class
Public Class MissionScript
    Inherits Script
    Private Shared currentMission As Mission
    Public Shared Sub StartMission(mission As Mission)
        If currentMission Is Nothing Then
            currentMission = mission
        Else
            UI.Screen.ShowSubtitle("当前正在执行一个差事")
        End If
    End Sub

    Private Sub MissionScript_Tick(sender As Object, e As EventArgs) Handles Me.Tick
        currentMission?.Start()
        currentMission = Nothing
    End Sub
End Class
Public Class EliminateMission
    Inherits Mission
    Private destinationBlip As Blip
    Private destinationText As String
    Private ReadOnly enermies As EntityCollection(Of Ped) = New EntityCollection(Of Ped)()
    Private ReadOnly centerPosition As Vector3
    Private ReadOnly enermyModels As Model()
    Private ReadOnly enermyWeapons As WeaponHash()
    Private generatedEnermies As Integer = 0
    Private ReadOnly maxEnermies As Integer = Pick(15, 8)
    Public Sub New(startPoint As Vector3, enermyModels As Model(), enermyWeapons As WeaponHash())
        centerPosition = startPoint
        Me.enermyModels = enermyModels
        Me.enermyWeapons = enermyWeapons
    End Sub
    Public Function GenerateEnermy(center_position As Vector3, model As Model, weapon As WeaponHash) As Boolean
        Dim distance As Single = Pick(10)
        Dim generationLocation As Vector3
        If World.GetSafePositionForPed(center_position.Around(distance), generationLocation, GetSafePositionFlags.OnlyNetworkSpawn) Then
            Dim enermy As Ped = World.CreatePed(model, generationLocation)
            If enermy Is Nothing Then
                Return False
            End If
            With enermy.AddBlip()
                .Sprite = BlipSprite.Enemy
                .Scale = 0.7
                .Color = BlipColor.Red
            End With
            If Pick({1, 2, 3}) = 1 Then
                enermy.SetCombatAttribute(CombatAttributes.PerfectAccuracy, True)
            End If
            enermy.SetCombatAttribute(CombatAttributes.Aggressive, False)
            enermy.SetCombatAttribute(CombatAttributes.EnableTacticalPointsWhenDefensive, True)
            enermy.CombatMovement = CombatMovement.Stationary
            enermy.Health = 500
            '[Function].Call(Hash.SET_PED_HAS_AI_BLIP, enermy, True)
            enermy.SetCombatAttribute(CombatAttributes.DisableInjuredOnGround, True)
            enermy.Weapons.Give(weapon, 150, True, True)
            enermy.RelationshipGroup = EnermyRelationshipGroup
            EntityManagement.AddController(New EnermyController(enermy))
            enermies.Add(enermy)
            'enermy.Task.Combat(PlayerPed)
            Return True
        Else
            Return False
        End If
    End Function

    Public Overrides Sub Start()
        destinationBlip = World.CreateBlip(centerPosition)
        destinationBlip.ShowRoute = True
        destinationText = World.GetStreetName(Me.centerPosition)
        UI.Screen.ShowSubtitle("前往 ~y~" + destinationText, 99999)
        While PlayerPed.Position.DistanceTo(centerPosition) > 50

            DisableWantedLevel()
            If generatedEnermies < maxEnermies AndAlso GenerateEnermy(centerPosition, Pick(enermyModels), Pick(enermyWeapons)) Then
                generatedEnermies += 1
            End If
            Wait(100)
        End While
        destinationBlip.Delete()
        While generatedEnermies < maxEnermies
            While Not GenerateEnermy(centerPosition, Pick(enermyModels), Pick(enermyWeapons))
                Wait(1000)
            End While
            generatedEnermies += 1
        End While
        UI.Screen.ShowSubtitle("干掉 ~r~敌人", 99999)
        While enermies.Count > 0
            enermies.Keep(KeepRule.MustAlive)
            For Each enermy As Ped In enermies
                enermy.CombatMovement = CombatMovement.Stationary
                enermy.SetCombatAttribute(CombatAttributes.Aggressive, False)
            Next

            Wait(1000)
            DisableWantedLevel()
        End While
        UI.Screen.ShowSubtitle(" ")
        DisplayMissionCompleteMessage()
        Game.Player.Money += 20000
        ResetWantedLevel()
    End Sub
    ''' <summary>
    ''' 刷出来的敌人控制器，拥有不会消失。
    ''' </summary>
    Private Class EnermyController
        Implements IEntityController
        Private ReadOnly enermy As Ped

        Public Sub New(enermy As Ped)
            Me.enermy = enermy
            enermy.IsEnemy = True
        End Sub
        Public ReadOnly Property Target As Entity Implements IEntityController.Target
            Get
                Return enermy
            End Get
        End Property

        Public Sub OnTick() Implements IEntityController.OnTick
            If Not enermy.IsInCombat Then
                enermy.Task.Combat(PlayerPed, taskThreatResponseFlags:=TaskThreatResponseFlags.CanFightArmedPedsWhenNotArmed)
            Else
                If enermy.IsRunning Then
                    enermy.CombatMovement = CombatMovement.Stationary
                    enermy.SetCombatAttribute(CombatAttributes.PerfectAccuracy, False)
                    enermy.Accuracy = 0

                End If
            End If

        End Sub

        Public Sub Disposing() Implements IEntityController.Disposing
        End Sub

        Public Function CanDispose() As Boolean Implements IEntityController.CanDispose
            If enermy.IsDead Then
                Return True
            Else
                Return False
            End If
        End Function
    End Class
End Class